match variable: case value: ---------- ---------- case value: ---------- ---------- case value: ---------- ---------- : : case _: ---------- ----------
n=int(input("Enter a number")) match n: case 0:print("Zero") case 1:print("One") case 2:print("Two") case 3:print("Three") case 4:print("Four") case 5:print("Five") case 6:print("Six") case 7:print("Seven") case 8:print("Eight") case 9:print("Nine") case _:print("Invalid input")
Enter a Number: 7 Seven
n=int(input("Enter a number")) match n: case 1:print("January") case 2:print("February") case 3:print("March") case 4:print("April") case 5:print("May") case 6:print("June") case 7:print("July") case 8:print("August") case 9:print("September") case 10:print("October") case 11:print("November") case 12:print("December") case _:print("Invalid input")
Enter a Number: 6 June
match variable: case value: ---------- ---------- case value | value: ---------- ---------- : : case _: ---------- ----------
n=input("Enter shirt size ") match n: case "S"|"s": print("small") case "M"|"m": print("medium") case "L"|"l": print("large") case _: print(f"cannot find {n} size shirt")
Enter shirt size: M medium
n=input("Enter color code ") match n: case "r"|"R": print("Red") case "g"|"G": print("Green") case "b"|"B": print("Blue") case _: print("White")
Enter color code: b Blue
match variable: case value: ---------- ---------- case value if condition: ---------- ---------- : : case _: ---------- ----------
Sal | Grade |
---|---|
>=50000 | A |
25000-50000 | B |
15000-25000 | C |
<15000 | D |
sal=int(input("Enter salary ")) match sal: case sal if sal>=50000 : print("A Grade") case sal if sal>=25000 : print("B Grade") case sal if sal>=15000 : print("C Grade") case _ : print("D Grade")
Enter Salary: 35000 B Grade
Percentage | Result |
---|---|
>=75 | passed with DT |
60-75 | passed with 1st class |
40-60 | passed |
<40 | failed |
p=int(input("Enter Percentage ")) match p: case p if p>=75 : print("Passed with DT") case p if p>=60 : print("Passed with 1st class") case p if p>=40 : print("Passed") case _ : print("Failed")
Enter Percentage: 65 Passed with 1st class
match variable: case value: ---------- ---------- case variable: ---------- ---------- : : case _: ---------- ----------
time=input("Enter time:") match time.split(":"): case [h,m] : print(f"{h} hours and {m} mins") case [h,m,s] : print(f"{h} hours {m} mins and {s} sec") case _ : print("Invalid format")
Enter time: 2:30 2 hours and 30 mins